package org.myy.learn.hot.finish.哈希;

import java.util.HashSet;
import java.util.Set;

public class 最长连续序列 {

    public int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums){set.add(num);}//去重
        int longest = 0;
        for (int num : set){
            if (!set.contains(num-1)){
                int curLen = 1;
                int curValue = num;  //ToDo 致命问题，必须新开curValue对象。num是用于循环的。
                while (set.contains(curValue+1)){
                    curLen += 1;
                    curValue += 1;
                }
                longest = Math.max(longest, curLen);
            }
        }
        return longest;
    }
}
